{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f9580f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "35b41d4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "###5.1.1  Series ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "188a7abc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4\n",
       "1    7\n",
       "2   -5\n",
       "3    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建简单序列\n",
    "obj = pd.Series([4,7,-5,3])\n",
    "obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c4cdbdb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4\n",
       "1    7\n",
       "2   -5\n",
       "3    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#自定义序列索引\n",
    "obj2 = pd.Series([4,7,-5,3],index=[1,2,3,4])\n",
    "obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2ef51e2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4,  7, -5,  3])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取值\n",
    "obj2.values\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5eaa3d5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([1, 2, 3, 4], dtype='int64')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取索引\n",
    "obj2.index\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9880b773",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Series.info of 0    4\n",
       "1    7\n",
       "2   -5\n",
       "3    3\n",
       "dtype: int64>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "de83d76b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n",
      "RangeIndex: 4 entries, 0 to 3\n",
      "Series name: None\n",
      "Non-Null Count  Dtype\n",
      "--------------  -----\n",
      "4 non-null      int64\n",
      "dtypes: int64(1)\n",
      "memory usage: 160.0 bytes\n"
     ]
    }
   ],
   "source": [
    "#查看详细信息\n",
    "obj.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8b7f16ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用标签索引\n",
    "obj2[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2508c280",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    4\n",
       "2    7\n",
       "4    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#条件筛选\n",
    "obj2[obj2 > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1b5ee571",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1     8\n",
       "2    14\n",
       "3   -10\n",
       "4     6\n",
       "dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#序列计算\n",
    "obj2*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c5035c58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#针对索引判断\n",
    "3 in obj2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "40d58300",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ohio       3500\n",
       "Texas      7100\n",
       "Oregon    16000\n",
       "dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用字典生成序列\n",
    "sdata = {'ohio':3500 , 'Texas':7100 , 'Oregon':16000}\n",
    "obj3 = pd.Series(sdata)\n",
    "obj3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "af0e6f34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ohio</th>\n",
       "      <td>3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Texas</th>\n",
       "      <td>7100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oregon</th>\n",
       "      <td>16000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            0\n",
       "ohio     3500\n",
       "Texas    7100\n",
       "Oregon  16000"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用序列生成数据框\n",
    "frame0 = pd.DataFrame(obj3)\n",
    "frame0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "ac245bdb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1     3500\n",
       "2     7100\n",
       "3    16000\n",
       "dtype: int64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ????索引没办法修改\n",
    "states = [1,2,3]\n",
    "obj4 = pd.Series(sdata.values(), index=states)\n",
    "obj4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "4a393daa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Texas']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e0cf56e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(sdata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "df61ac17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(data=[2,3,4], index=states)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "a9717bda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    False\n",
       "2    False\n",
       "3    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查找缺失值\n",
    "pd.isnull(obj4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "5d21fdd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    False\n",
       "2    False\n",
       "3    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj4.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "12972b0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1     7000\n",
       "2    14200\n",
       "3    32000\n",
       "dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj4+obj4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "0c0b99d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "state\n",
       "ohio       3500\n",
       "Texas      7100\n",
       "Oregon    16000\n",
       "Name: population, dtype: int64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#给序列命名\n",
    "obj3.name = 'population'\n",
    "obj3.index.name = 'state'\n",
    "obj3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "c5d5655f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4\n",
       "1    7\n",
       "2   -5\n",
       "3    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "0332261a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Bob      4\n",
       "Steve    7\n",
       "Jeff    -5\n",
       "Ryan     3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.index = ['Bob','Steve','Jeff','Ryan']\n",
    "obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "6adf8f18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['Bob', 'Steve', 'Jeff', 'Ryan'], dtype='object')"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "obj.index\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90dd5935",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "c05c85d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 5.1.2 DataFrame ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "c5dd51af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ohio</th>\n",
       "      <td>3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Texas</th>\n",
       "      <td>7100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oregon</th>\n",
       "      <td>16000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            0\n",
       "state        \n",
       "ohio     3500\n",
       "Texas    7100\n",
       "Oregon  16000"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "866a7235",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>state</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ohio</th>\n",
       "      <td>3500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Texas</th>\n",
       "      <td>7100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oregon</th>\n",
       "      <td>16000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            0\n",
       "state        \n",
       "ohio     3500\n",
       "Texas    7100\n",
       "Oregon  16000"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame0.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "1968850b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Texas</th>\n",
       "      <th>ohio</th>\n",
       "      <th>Oregon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [Texas, ohio, Oregon]\n",
       "Index: []"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "frame1 = pd.DataFrame(obj3, columns= ['Texas','ohio','Oregon'])\n",
    "frame1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7884b3a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
